package com.social.instadownloader.network.download;

import java.io.File;

import org.apache.commons.io.FilenameUtils;

import android.app.Activity;

import com.social.instadownloader.network.FetcherData;
import com.social.instadownloader.util.DialogUtil;
import com.social.instadownloader.util.DialogUtil.OnDialogListener;
import com.social.instadownloader.util.DialogUtility;
import com.social.instadownloader.util.InstaFileManager;
import com.social.instadownloader.util.SdcardUtils;
import com.social.instadownloader.util.TextUtil;

public class InstaDownloader {

	private Activity mActivity;
	
	public InstaDownloader(Activity activity){
		mActivity = activity;
	}
	
	public void downloadImage(FetcherData fetcherData){
		showInputDialogImageName(fetcherData);
	}
	public void downloadVideo(FetcherData fetcherData){
		showInputDialogVideoName(fetcherData);
	}
	
	/**
     * Show input dialog image name.
     */
    private void showInputDialogImageName(final FetcherData fetcherData) {
        if (fetcherData != null && fetcherData.getImageUrl() != null
                && !fetcherData.getImageUrl().trim().equals("")) {
            DialogUtil.showInputDialog(mActivity, "Enter Image Name", InstaFileManager.INSTA_DOWNLOADER_FOLDER,
                    new OnDialogListener() {

                        @Override
                        public void onPositive(String result) {
                            if (result != null && !result.trim().equals("")) {
                                result +=
                                        "."
                                                + FilenameUtils.getExtension(FilenameUtils
                                                        .getName(fetcherData.getImageUrl()));
                                downloadInstaImage(fetcherData,result);
                            }
                        }

                        @Override
                        public void onCancel() {}
                    }, TextUtil.getFileName(fetcherData.getImageUrl()));
        } else {
            DialogUtil.showConfirmDialog(mActivity, "Warning", "Sorry, Image Not Found !");
        }

    }
    
    /**
     * Download insta image.
     * 
     * @param fileName the file name
     */
    private void downloadInstaImage(final FetcherData fetcherData,final String fileName) {
        if (fetcherData != null && fetcherData.getImageUrl() != null) {
            final String absolutePath = InstaFileManager.INSTA_DOWNLOADER_FOLDER + fileName;
            final File file = new File(absolutePath);
            if (file.exists()) {
                String message = absolutePath + " exist ,are you sure continue download?";
                DialogUtil.showConfirmDialog(mActivity, "Download Image", message,
                        new OnDialogListener() {

                            @Override
                            public void onPositive(String result) {
                                file.delete();
                                if (!SdcardUtils.hasStorage()) {
                                    DialogUtility.alert(mActivity, "Canot start download.");
                                } else {
                                	new DownloadWithProgress(mActivity).runDownload(fetcherData.getImageUrl(), fileName);
                                }
                            }

                            @Override
                            public void onCancel() {}
                        });
            } else {
                String message = "do you want to download this image?";
                DialogUtil.showConfirmDialog(mActivity, "Download Image", message,
                        new OnDialogListener() {

                            @Override
                            public void onPositive(String result) {
                                if (!SdcardUtils.hasStorage()) {
                                    DialogUtility.alert(mActivity, "Canot start download.");
                                } else {
                                    new DownloadWithProgress(mActivity).runDownload(fetcherData.getImageUrl(), fileName);
                                }
                            }

                            @Override
                            public void onCancel() {}
                        });
            }

        }
    }
    
    /**
     * Show input dialog video name.
     */
    private void showInputDialogVideoName(final FetcherData fetcherData) {
        if (fetcherData != null && fetcherData.getVideoUrl() != null
                && !fetcherData.getVideoUrl().trim().equals("")) {
            DialogUtil.showInputDialog(mActivity, "Enter Video Name", InstaFileManager.INSTA_DOWNLOADER_FOLDER,
                    new OnDialogListener() {

                        @Override
                        public void onPositive(String result) {
                            if (result != null && !result.trim().equals("")) {
                                result +=
                                        "."
                                                + FilenameUtils.getExtension(FilenameUtils
                                                        .getName(fetcherData.getVideoUrl()));
                                downloadInstaVideo(fetcherData,result);
                            }
                        }

                        @Override
                        public void onCancel() {}
                    }, TextUtil.getFileName(fetcherData.getImageUrl()));
        } else {
            DialogUtil.showConfirmDialog(mActivity, "Warning", "Sorry, Video Not Found !");
        }
    }
    
    /**
     * Download insta video.
     * 
     * @param fileName the file name
     */
    private void downloadInstaVideo(final FetcherData fetcherData,final String fileName) {
        if (fetcherData != null && fetcherData.getVideoUrl() != null) {
            final String absolutePath = InstaFileManager.INSTA_DOWNLOADER_FOLDER + fileName;
            if (new File(absolutePath).exists()) {
                String message = absolutePath + " exist ,are you sure continue download?";
                DialogUtil.showConfirmDialog(mActivity, "Download Video", message,
                        new OnDialogListener() {

                            @Override
                            public void onPositive(String result) {
                                new File(absolutePath).delete();
                                if (!SdcardUtils.hasStorage()) {
                                    DialogUtility.alert(mActivity, "Canot start download.");
                                } else {
                                	new DownloadWithProgress(mActivity).runDownload(fetcherData.getVideoUrl(), fileName);
                                }
                            }

                            @Override
                            public void onCancel() {}
                        });
            } else {
                String message = "do you want to download this video?";
                DialogUtil.showConfirmDialog(mActivity, "Download Video", message,
                        new OnDialogListener() {

                            @Override
                            public void onPositive(String result) {
                                if (!SdcardUtils.hasStorage()) {
                                    DialogUtility.alert(mActivity, "Canot start download.");
                                } else {
                                	new DownloadWithProgress(mActivity).runDownload(fetcherData.getVideoUrl(), fileName);
                                }
                            }

                            @Override
                            public void onCancel() {}
                        });
            }

        }
    }
}
